<?php

/**
 *  Uses some rewrite magic to display a year or months posts in a tumbly way.
 *  Copy tumble.php to your theme folder and make the changes you want. 
 *
 **/

class tumble_archive extends Plugin
{
	
	public function action_update_check()
	{
	 	Update::add( 'tumbleArchive', '8f6a8c92-d54e-4dae-bb72-ec3101a201a7', $this->info->version );
	}
	
	/**
	 * Makes sure everything is setup proper.
	 **/
	public function action_plugin_activation( $file )
	{
		if(Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) {
		/* Nothing at the moment  */
		}
	}
	
	public function filter_rewrite_rules( $rules )
	{		
		$rules[] = new RewriteRule(array(
	               	'name' => 'display_archive',
					'parse_regex' => '%archives(?:/(?P<year>\\d{4}))?(?:/(?P<month>\\d{2}))?/?$%i',
					'build_str' => 'archive(/{$year})(/{$month})',
					'handler' => 'PluginHandler',
					'action' => 'tumbleHandler',
					'priority' => 1,
					'rule_class' => 1,
					'is_active' => 1,
					 'description' => 'Tumble archive rule.')
	                );
		return $rules;
	}

	/**
	 * Plugin init action, executed when plugins are initialized.
	 */
	public function action_init()
	{
		$this->add_template( 'tumble', dirname(__FILE__) . '/tumble.php' );
		Stack::add( 'template_stylesheet', array( $this->get_url() . '/tumble.css', 'screen' ), 'tumbleArchive' );

        /**/
	}
	
	public function theme_header( $theme ) 
    {
		if( $theme->template_engine->assigned( 'pages' ) ) {
            $post = new Post;
            $post->title = 'Archives';
            $post->slug = 'archives';
            $theme->pages[] = $post;
		}
	}

	/**
	 * Add actions to the plugin page for this plugin
	 * @param array $actions An array of actions that apply to this plugin
	 * @param string $plugin_id The string id of a plugin, generated by the system
	 * @return array The array of actions to attach to the specified $plugin_id
	 **/
	public function filter_plugin_config( $actions, $plugin_id )
	{
		if ( $plugin_id == $this->plugin_id() ) {
			$actions[] = 'Configure';
		}

		return $actions;
	}
	
	public function action_plugin_act_tumbleHandler($handler) 
	{	
	
		//Cache::expire( "tumble_archive" );
		//if ( Cache::has( "tumble_archive" ) ) {
		//	$theme->tumblearchive = Cache::get( "tumble_archive" );
		//} else {
			//$theme->tumblearchive = $this->fetch_archive();
		//	Cache::set( "tumble_archive", $theme->tumblearchive );

		//}
		//$theme->display( 'tumble' );
	
	
		extract((array) $handler->handler_vars, EXTR_PREFIX_ALL, 'glob' ); // extract the rewrite vars.
		
		$get = array( 'content_type' => Post::type( 'entry' ),
									'status' => Post::status( 'published' ),
									'nolimit' => true );
							
		if( isset( $glob_year ) ) $get['year'] = $glob_year; // Do we have a year?
		if( isset( $glob_month ) ) $get['month'] = $glob_month; // Do we have a month?
		
		$archive_posts = Posts::get( $get ); // Fetch!
		
		$archive = array();
		
		foreach($archive_posts as $post) {
			$year = ( $post->pubdate->get( 'Y' ) );
			$month = ( $post->pubdate->get( 'm' ) );
			$day = ( $post->pubdate->get( 'd' ) );
			if( !isset( $archive[ "{$year}" ][ "count" ] ) ) 
				$archive[ "{$year}" ][ "count" ] = 0;
			if( !isset( $archive[ "{$year}" ][ "months" ][ "{$month}" ][ "count" ] ) ) 
				$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "count" ] = 0;
			if( !isset( $archive[ "{$year}" ][ "months" ][ "{$month}" ][ "days" ][ "{$day}" ][ "count" ] ) ) 
				$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "days" ][ "{$day}" ][ "count" ] = 0;

			$archive[ "{$year}" ][ "url" ] = 
				URL::get( 'display_entries_by_date', array( 'year' => $year ) );
			$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "url" ] = 
				URL::get( 'display_entries_by_date', array( 'year' => $year, 'month' => $month ) );
			$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "days" ][ "{$day}" ][ "url" ] = 
				URL::get( 'display_entries_by_date', array( 'year' => $year, 'month' => $month, 'day' => $day ) );


			$archive[ "{$year}" ][ "count" ]++;
			$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "count" ]++;
			$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "days" ][ "{$day}" ][ "count" ]++;
			$archive[ "{$year}" ][ "months" ][ "{$month}" ][ "days" ][ "{$day}" ][ "posts" ][] = $post;
		}
		
		
		$theme = Themes::create();
		$theme->tumblearchive = $archive;
		$theme->display('tumble');
	}	
}
?>
